/*
 * @Author: your name
 * @Date: 2020-05-09 10:51:39
 * @LastEditTime: 2021-05-11 19:59:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nccloud/src/gl/public/components/FilterPanel/utils.js
 */


 /*
 * @method   是否是undefined
 * @author   add by yangguoqiang @18/05/31
 * @params
 *     origin     {Any}      元数据
 * @return   {Boolean}          true/false
 * @demo     isUndefined(any)
 */
export function isUndefined(origin) {
    return typeof origin === 'undefined' || origin === void 0;
}

// 产生随机key值
export function getRandom() {
    return String(new Date().getTime()).slice(-5) + (new Date().getTime()).toString(12);
}


/* 检测类型是否为数组 */
export function isFunction(param) {
    return Object.prototype.toString.call(param).slice(8, -1) === 'Function';
}

/*
 * @method   if条件下为false   除去NaN、0、-0、false   剩余undefined、null、""
 * @author   add by yangguoqiang @18/03/19
 * @params
 *     condition     {any}
 * @return   {boolean}       返回ture/false
 * @demo     isWrong('')    因为后台返回数据不规范
 */
export function isWrong(param) {
    return typeof param === 'undefined' || param === null || param === '';
}

export function isNullOrVoid(param) {
  return typeof param === 'undefined' || param === null;
}


/**
 * 筛选数据的方法
 * @param {*} allRows 总数据
 * @param {*} filterFactor 过滤条件
 * @param {*} filterCustomColData 自定义列传入数据
 *
 * filterResult 筛选到结果
 * filteredList 已经筛选出来的行的index集合
 * filteredAttrList 已经筛选出来结果的筛选条件项集合
 * locationResult 定位到的结果
 */
export function getFilterData({ allRows, filterFactor, filterCustomColData,preOrNext,locationResultBack }) {
    return filterFactor.reduce(
      (currentFactor, factor) => {
        let {
          // 以下是模版字段和筛选条件
          attrcode,
          itemtype,
          factorValue,
          scale: metaScale,
          languageMeta = [],
          itemKey,
          datatype,
          options,
          multiple
        } = factor;
        if (factorValue) {
          const { filterResult, filteredList, filteredAttrList } = currentFactor;
          let activeIndex=0;
          allRows.forEach((row, index) => {
            const { values, status, rowid, rowId } = row;
            if (status !== 3) {
              let tableItemValue = '';
              // 自定义列穿进来的数据判断
              if (filterCustomColData && filterCustomColData[attrcode]) {
                if (filterCustomColData[attrcode][rowid]) {
                  // cardtable和edittable进行筛选的情况
                  tableItemValue = filterCustomColData[attrcode][rowid];
                }
                if (filterCustomColData[attrcode][rowId]) {
                  // simpletable进行筛选的情况
                  tableItemValue = filterCustomColData[attrcode][rowId];
                }
              } else {
                // 不是自定义列的情况
                //values[attrcode]  row[attrcode]
                if (row[attrcode]) {
                  const valueScale = row[attrcode].scale;
                  // 精度判断数据和模版的情况
                  const scale = isWrong(valueScale) || valueScale == '-1' ? metaScale || '0' : valueScale;
                  // tableItemValue = handleBrowse.call(
                  //   this,
                  //   itemtype,
                  //   row[attrcode].display,
                  //   row[attrcode].value,
                  //   scale,
                  //   attrcode,
                  //   languageMeta,
                  //   values,
                  //   datatype,
                  //   options,
                  //   multiple
                  // );
                  // tableItemValue = row[attrcode];
                  let assItems = '';
                  if(attrcode === 'accAssItems'){ //期初余额辅助核算特殊处理
                    row[attrcode].forEach((item, i)=>{
                      assItems += item.name + ' ';
                    })
                  }
                  tableItemValue = isNullOrVoid(row[attrcode].display) ? (row[attrcode].value || assItems) : (row[attrcode].display || row[attrcode].value);
                }
              }
              // 为了不区分大小写统一将字符串转为小写
              isWrong(tableItemValue) && (tableItemValue = '');
              const LowerTableItemValue = tableItemValue.toLowerCase();
              const LowerFactorValue = factorValue.toLowerCase();
              if (
                tableItemValue &&
                LowerTableItemValue.includes(LowerFactorValue)
              ) {
                !filteredAttrList.includes(itemKey) &&
                  filteredAttrList.push(itemKey);
                !filteredList.includes(index) && filterResult.push(row);
                !filteredList.includes(index) && filteredList.push(index);

                
              }
            }
          });
          locationResultBack.currentAttrcode=attrcode;
          locationResultBack.currentIndex=filteredList;
          locationResultBack.searchValue=factorValue;
          if(locationResultBack.current_Index===''||locationResultBack.activeIndex===''){ //!preOrNext&&
            locationResultBack.activeIndex=0;
          }else if(preOrNext=='pre'){
            if(locationResultBack.activeIndex>0&&(filteredList[Number(locationResultBack.activeIndex)-1]||filteredList[Number(locationResultBack.activeIndex)-1]==0)){
              locationResultBack.isFirst=false;
              locationResultBack.activeIndex = Number(locationResultBack.activeIndex)-1;
            }else{
              locationResultBack.isFirst=true;
            }
            locationResultBack.isLast=false;
          }else  if(preOrNext=='next'){
            if(filteredList[Number(locationResultBack.activeIndex)+1]){
              locationResultBack.isLast=false;
              locationResultBack.activeIndex = Number(locationResultBack.activeIndex) + 1;
            }else{
              locationResultBack.isLast=true;
            }
            locationResultBack.isFirst=false;
          }else{
            if(filteredList[Number(locationResultBack.activeIndex)+1]){
              locationResultBack.isLast=false;
              locationResultBack.activeIndex = Number(locationResultBack.activeIndex)+ 1;
              
            }else{
              locationResultBack.isLast=true;
            }
            locationResultBack.isFirst=false;
          }
          locationResultBack.current_Index=filteredList[Number(locationResultBack.activeIndex)];
          currentFactor.locationResultBack=locationResultBack;
        }else{
          if(locationResultBack){
            locationResultBack.searchValue='';
          }
          currentFactor.locationResultBack=locationResultBack;
        }
        return currentFactor;
      },
      { filterResult: [], filteredList: [], filteredAttrList: [] }
    );
  }
